﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity;
using School.Entities;
using School.Common;
using School.Organization;

namespace School.Pages.Organization
{
    public partial class Grades : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var xx = (IQueryable<int>)Session["privilage"];

                if (xx.Contains(1304) == true)
                {
                    GridView1.DataSource = School.Common.Structure.GetGrades().Select(x => new { Name = x.Name, Id = x.Id, Description = x.Description, CName = x.ORG_Stage.ORG_Certificate.ORG_Campus.Name, CeName = x.ORG_Stage.ORG_Certificate.Name, SName = x.ORG_Stage.Name, x.Code });
                    GridView1.DataBind();
                    lblcode.Visible = false;
                    if (!IsPostBack)
                    {
                        LstCampusesBind();

                        //if (School.Common.Structure.GetCertificatesByCampus(School.Common.Structure.GetCampuses().First().Id).Count() > 0)
                        //{
                        //    LstCertificates.DataSource = School.Common.Structure.GetCertificatesByCampus(School.Common.Structure.GetCampuses().First().Id);
                        //    //LstCertificates.DataValueField = "Id";
                        //    //LstCertificates.DataTextField = "Name";
                        //    LstCertificates.DataBind();
                        //}

                        //if (LstCertificates.Items.Count > 0)
                        //{
                        //    LstStages.DataSource = School.Common.Structure.GetStagesByCertificate(long.Parse(LstCertificates.Items[0].Value));
                        //    //LstStages.DataValueField = "Id";
                        //    //LstStages.DataTextField = "Name";
                        //    LstStages.DataBind();
                        //}

                        long id = 0;

                        if (long.TryParse(Request.QueryString["Id"], out id) && !IsPostBack)
                        {
                            var grade = Grade.Single(id);

                            if (grade != null)
                            {
                                LstCampuses.SelectedValue = grade.ORG_Stage.ORG_Certificate.ORG_Campus.Id.ToString();

                                LstCertificates.DataSource = School.Common.Structure.GetCertificatesByCampus(grade.ORG_Stage.ORG_Certificate.ORG_Campus.Id);
                                //LstCertificates.DataValueField = "Id";
                                //LstCertificates.DataTextField = "Name";
                                LstCertificates.DataBind();

                                LstCertificates.SelectedValue = grade.ORG_Stage.Certificate_Id.ToString();

                                LstStages.DataSource = School.Common.Structure.GetStagesByCertificate(grade.ORG_Stage.Certificate_Id);
                                //LstStages.DataValueField = "Id";
                                //LstStages.DataTextField = "Name";
                                LstStages.DataBind();

                                LstStages.SelectedValue = grade.Stage_Id.ToString();

                                TxtName.Text = grade.Name;
                                TxtDescription.Text = grade.Description;
                                TxtCode.Text = grade.Code;
                            }
                            else
                            {
                                Response.Redirect("Grades.aspx");
                            }
                        }

                        LstCertificates.Items.Insert(0, new ListItem() { Value = "0", Text = "اختيار" });
                        LstStages.Items.Insert(0, new ListItem() { Value = "0", Text = "اختيار" });
                    }
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }

        private void LstCampusesBind()
        {
            LstCampuses.DataSource = School.Common.Structure.GetCampuses();
            //LstCampuses.DataValueField = "Id";
            //LstCampuses.DataTextField = "Name";
            LstCampuses.Items.Insert(0, new ListItem("اختيار", "-1"));
            LstCampuses.DataBind();
        }

        protected void LstCampuses_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (LstCampuses.SelectedIndex > 0)
            {
                LstCertificates.DataSource = School.Common.Structure.GetCertificatesByCampus(long.Parse(LstCampuses.SelectedValue));
                ddlBind(LstCertificates);
            }

            //LstCertificates.DataValueField = "Id";
            //LstCertificates.DataTextField = "Name";
            //LstCertificates.DataBind();

            //LstCertificates.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الشهادة" });

            //LstStages.DataSource = School.Common.Structure.GetStagesByCertificate(long.Parse(LstCertificates.SelectedValue));
            //LstStages.DataValueField = "Id";
            //LstStages.DataTextField = "Name";
            //LstStages.DataBind();
            //LstStages.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر المرحلة" });
        }
        private void ddlBind(DropDownList ddl)
        {
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "0"));
            ddl.DataBind();
        }
        protected void LstCertificates_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (LstCertificates.SelectedIndex > 0)
            {

                LstStages.DataSource = School.Common.Structure.GetStagesByCertificate(long.Parse(LstCertificates.SelectedValue));
                ddlBind(LstStages);
                //LstStages.DataValueField = "Id";
                //LstStages.DataTextField = "Name";
                //LstStages.DataBind();
                //LstStages.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر المرحلة" });
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            long id = 0;

            if (long.TryParse(Request.QueryString["Id"], out id))
                Grade.Update(new ORG_Grade() { Id = id, Name = TxtName.Text, Description = TxtDescription.Text, Code = TxtCode.Text, Stage_Id = long.Parse(LstStages.SelectedValue) });
            else
                Grade.Add(new ORG_Grade() { Name = TxtName.Text, Description = TxtDescription.Text, Code = TxtCode.Text, Stage_Id = long.Parse(LstStages.SelectedValue) });

            Response.Redirect("Grades.aspx");
        }

        protected void LstStages_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (LstStages.SelectedIndex > 0)
            {
                lblcode.Text = "الكود=" + School.Common.Structure.Get_stage_code(Convert.ToInt32(LstStages.SelectedValue));
                lblcode.Visible = true;
            }
        }
    }
}